package com.baiytfp.hf.HFDataExchange.action.service;

import java.sql.Connection;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baiytfp.hf.HFDataExchange.action.dao.PasswordRetrievalDAOImpl;
import com.baiytfp.hf.HFDataExchange.common.model.base.HFSysParams;
import com.baiytfp.hf.HFDataExchange.common.model.table.Password_retrieval;
import com.baiytfp.hf.HFDataExchange.common.model.table.User_info;
import com.baiytfp.hf.HFDataExchange.common.utils.MailTool;
import com.baiytfp.hf.baiytfp.common.db.DBManager;
import com.baiytfp.hf.baiytfp.common.util.DateUtil;
import com.baiytfp.hf.baiytfp.framework.base.BaseService;

@Service
public class PasswordRetrievalService extends BaseService {

	@Resource(name= HFSysParams.DB_TYPE + "PasswordRetrievalDAOImpl")
	private PasswordRetrievalDAOImpl passwordRetrievalDAOImpl;
	
	@Autowired
	private PasswordRetrievalService passwordRetrievalService;
	
	private String getKey() throws Exception {
		String uuid = UUID.randomUUID().toString();
		uuid = uuid.replace("-", "");
		if(getPasswordRetrievalByKey(uuid) != null)
			return getKey();
		return uuid;
	}
	
	/**
	 * 发送邮件
	 * @param email
	 * @param user_code
	 * @return
	 * @throws Exception
	 */
//	@Transcation
	public long sendmail(Password_retrieval password_retrieval, User_info user, String webroot) throws Exception {
		password_retrieval.setRetrieval_key(getKey());
		password_retrieval.setUser_code(user.getUser_code());
		password_retrieval.setCreate_time(DateUtil.getCurrentDT());
		long res = addPasswordRetrieval(password_retrieval);
		//发送邮件
		String url = webroot+"/view/index/login.do?key="+password_retrieval.getRetrieval_key();
		String title = "密码重置验证";
		String content = user.getUser_name() + ", 您好："
				+ "<br /><br />&nbsp;&nbsp;<a href=\""+url+"\">我们收到了来自您的密码重置请求, 请尽快点击此链接进行重置</a>"
				+ "<br />&nbsp;&nbsp;如果您并没有申请本次密码重置, 请不要点击此邮件中的任何内容!"
				+ "<br /><br />&nbsp;&nbsp;<b>请注意:</b> 该链接将在1小时后过期, 请尽快验证";
		new SendEmailThread(title, content, password_retrieval.getEmail()).start();
		return res;
	}
	
	/**
	 * 更新密码
	 * @param id PasswordRetrieval id
	 * @param new_password
	 * @param user_code
	 * @return
	 * @throws Exception
	 */
//	@Transcation
	public long updatePassword(String id, String new_password, String user_code) throws Exception {
		passwordRetrievalService.updatePsd(user_code, new_password);
		return passwordRetrievalService.deletePasswordRetrieval(id);
	}
	
	/**
	 * 更改密码
	 * @param user_code
	 * @return
	 * @throws Exception
	 */
//	@Transcation
	public long updatePsd(String user_code, String new_password) throws Exception {
		long res = 0;
		Connection conn = DBManager.getConnection();
		try {
			res = passwordRetrievalDAOImpl.updatePsd(user_code, new_password, conn);
		} finally {
			DBManager.close(conn);
		}
		return res;
	}
	
	/**
	 * 根据邮箱获取员工信息
	 * @param email
	 * @return
	 * @throws Exception
	 */
//	@Transcation(use=false)
	public User_info getStaffByEmail(String email) throws Exception {
		User_info ui = null;
		Connection conn = DBManager.getConnection();
		try {
			ui =  passwordRetrievalDAOImpl.getStaffByEmail(email,conn);
		} finally {
			DBManager.close(conn);
		}
		return ui;
	}
	
	//-------
//	@Transcation(use=false)
	public Password_retrieval getPasswordRetrievalById(String id) throws Exception {
		Password_retrieval pr = null;
		Connection conn = DBManager.getConnection();
		try {
			pr = passwordRetrievalDAOImpl.getPasswordRetrievalById(id, conn);
		} finally {
			DBManager.close(conn);
		}
		return pr;
	}
//	@Transcation(use=false)
	public Password_retrieval getPasswordRetrievalByKey(String key) throws Exception {
		Password_retrieval pr = null;
		Connection conn = DBManager.getConnection();
		try {
			pr = passwordRetrievalDAOImpl.getPasswordRetrievalByKey(key, conn);
		} finally {
			DBManager.close(conn);
		}
		return pr;
	}
	
//	@Transcation
	public long deletePasswordRetrieval(String ids) throws Exception {
		long res = 0;
		Connection conn = DBManager.getConnection();
		try {
			res = passwordRetrievalDAOImpl.deletePasswordRetrieval(ids, conn);
		} finally {
			DBManager.close(conn);
		}
		return res;
	}
	
//	@Transcation
	public long addPasswordRetrieval(Password_retrieval password_retrieval) throws Exception {
		long res = 0;
		Connection conn = DBManager.getConnection();
		try {
			res = passwordRetrievalDAOImpl.addPasswordRetrieval(password_retrieval, conn);
		} finally {
			DBManager.close(conn);
		}
		return res;
	}
	
	/**
	* @ClassName: SendEmailThread 
	* @Description: 发送邮件线程
	* @author 罗赵树
	* @date 2017年10月19日 下午7:01:45 by linksoft
	*
	*/
	class SendEmailThread extends Thread {
		private String title;
		private String content;
		private String email;
		public SendEmailThread(String title, String content, String email) {
			this.content = content;
			this.title = title;
			this.email = email;
		}
		
		@Override
		public void run() {
			try {
				MailTool.sendEmail(title, content, email);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
